package com.pm.controller.negative;

import com.github.pagehelper.PageInfo;
import com.pm.common.Response;
import com.pm.common.annotation.CurrentUser;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.negative.NegativeChecklist;
import com.pm.entity.po.security.User;
import com.pm.entity.vo.NegativeChecklistVo;
import com.pm.service.negative.NegativeChecklistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@Api(tags = "负面清单")
@UserLoginToken
@RestController
@RequestMapping("/negativeChecklist")
public class NegativeChecklistController {

    @Autowired
    NegativeChecklistService negativeChecklistService;

    /**
     * 添加负面清单
     * @return
     */
    @ApiOperation("添加负面清单")
    @PostMapping("/addChecklist")
    @ResponseBody
    @MyLog(value = "新增负面清单")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> addChecklist(@RequestBody NegativeChecklist negativeChecklist, @CurrentUser User user) {
        Response<Integer> response = null;
        Integer result = negativeChecklistService.addChecklist(negativeChecklist, user);
        if (result > 0) {
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }

    /**
     * 查询全部部门
     * @return
     */
    @ApiOperation("查询负面清单")
    @PostMapping("/getChecklist")
    @ResponseBody
    public Response<PageInfo<NegativeChecklistVo>> getAll(NegativeChecklist negativeChecklist) {
        PageInfo<NegativeChecklistVo> task = negativeChecklistService.getChecklist(negativeChecklist);
        Response<PageInfo<NegativeChecklistVo>> response = new Response<>(ExpCodeState.query_data_success, task);
        return response;
    }

    @ApiOperation("删除负面清单")
    @PostMapping("/DeleteChecklist")
    @ResponseBody
    @MyLog(value = "删除负面清单")
    public Response<Integer> DeleteChecklist(@RequestParam("id") Integer id) {
        boolean res = negativeChecklistService.deleteChecklist(id);
        if (res) {
            return new Response<>(ExpCodeState.remove_data_success);
        } else {
            return new Response<>(ExpCodeState.remove_data_failed);
        }
    }

    @ApiOperation("修改负面清单")
    @PostMapping("/updateChecklist")
    @ResponseBody
    @MyLog(value = "修改负面清单")
    public Response<Integer> updateChecklist(@RequestBody NegativeChecklist negativeChecklist, @CurrentUser User user) {
        boolean res = negativeChecklistService.updateChecklist(negativeChecklist, user);
        if (res) {
            return new Response<>(ExpCodeState.update_data_success);
        } else {
            return new Response<>(ExpCodeState.update_data_failed);
        }
    }

    /**
     * 根据id查部门
     * @return
     */
    @ApiOperation("Id查询负面清单")
    @PostMapping("/getAllChecklistId")
    @ResponseBody
    public Response<NegativeChecklistVo> getAllChecklistId(Integer id) {
        NegativeChecklistVo negativeChecklist = negativeChecklistService.getAllChecklistId(id);
        Response<NegativeChecklistVo> response = new Response<>(ExpCodeState.query_data_success, negativeChecklist);
        return response;
    }

}
